//
//  BLlocalStore.m
//  BasketBallLeagues
//
//  Created by 小禹宝宝 on 2017/5/24.
//  Copyright © 2017年 kenshin. All rights reserved.
//

#import "BLlocalStore.h"
#import "BLApi.h"

#define kLocalLanuchImages @"LocalLanuchImages.plist"

@implementation BLlocalStore
+ (void)storeTKAccountInfo:(TKAccountInfo *)accountInfo {
    //获取沙盒目录(路径)
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathDirectory = [paths objectAtIndex:0];
    NSString *filePath = [pathDirectory stringByAppendingPathComponent:@"TKAccountInfo.data"];
    NSLog(@"filePath ------%@",filePath);    //归档 archiveRootObject会调用encodeWithCoder:
    BOOL suc = [NSKeyedArchiver archiveRootObject:accountInfo toFile:filePath];
    if (suc) {
        NSLog(@"归档成功");
    } else {
        NSLog(@"归档失败");
        
    }
    
}

+ (TKAccountInfo *)readaccountInfo {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathDirectory = [paths objectAtIndex:0];
    NSString *filePath = [pathDirectory stringByAppendingPathComponent:@"TKAccountInfo.data"];
    
    NSLog(@"filePath ------%@",filePath);
    return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

+ (BOOL)storeLanuchImages:(NSArray *)imageArray {
BOOL success = [imageArray writeToFile:[CacheDirectory stringByAppendingString:kLocalLanuchImages] atomically:YES];
    return success;
}

+ (NSArray *)readLanuchImages {
    NSArray *imageArray = [NSArray arrayWithContentsOfFile:[CacheDirectory stringByAppendingString:kLocalLanuchImages]];

    return imageArray;
}

+ (void)requestLanuchImages {
    @weakify(self)
    [BLApi POST:URL_LanuchG_start_page parameters:nil decodeClass:[BLStartImageResponse class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        @strongify(self)
        if (!error) {
            BLStartImageResponse *response = responseObject;
            if (!response.errcode) {
                NSArray *imgArray = response.imgs;
                [BLlocalStore storeLanuchImages:imgArray];
                
            } else {
                [self ks_showToast:response.errmsg];
            }
        }
    }];

}

+ (void)storeLocationCity:(NSString *)city{
    [[NSUserDefaults standardUserDefaults] setObject:city forKey:@"city"];
}

+ (NSString *)readLocationCity {
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
}

@end

@implementation BLStartImageResponse


@end
